import * as Cesium from 'cesium'

type Bounds = {
  viewSwLng: number
  viewSwLat: number
  viewNeLng: number
  viewNeLat: number
}

/**
 * 获取相机视角范围
 * @param camera
 * @returns
 */
export function getViewBounds(viewer: Cesium.Viewer): Bounds | null {
  let viewport = viewer.camera?.computeViewRectangle()
  if (typeof viewport === 'undefined') {
    const canvas = viewer.scene.canvas
    const upperLeft = new Cesium.Cartesian2(0, 0)
    const lowerRight = new Cesium.Cartesian2(canvas.clientWidth, canvas.clientHeight)

    const ellipsoid = viewer.scene.globe.ellipsoid
    const upperLeft3 = viewer.camera.pickEllipsoid(upperLeft, ellipsoid)!
    const lowerRight3 = viewer.camera.pickEllipsoid(lowerRight, ellipsoid)!

    const upperLeftCartographic = viewer.scene.globe.ellipsoid.cartesianToCartographic(upperLeft3)
    const lowerRightCartographic = viewer.scene.globe.ellipsoid.cartesianToCartographic(lowerRight3)

    const west = upperLeftCartographic.longitude
    const south = lowerRightCartographic.longitude
    const east = lowerRightCartographic.latitude
    const north = upperLeftCartographic.latitude

    viewport = new Cesium.Rectangle(west, south, east, north)
  }
  return {
    viewSwLng: Cesium.Math.toDegrees(viewport.west),
    viewSwLat: Cesium.Math.toDegrees(viewport.south),
    viewNeLng: Cesium.Math.toDegrees(viewport.east),
    viewNeLat: Cesium.Math.toDegrees(viewport.north),
  }
}

export function getIcon(key: string | number, iconMaps: { [x: string]: any }) {
  console.log('🚀 ~ getIcon ~ key:', key, iconMaps)
  // return `http://23.210.227.13:25082/prod-api/${iconMaps[key]}&dcqc-tk=me-debug`
  // return 'https://user-images.githubusercontent.com/14011726/94132137-7d4fc100-fe7c-11ea-8512-69f90cb65e48.gif'
  return '/public/images/demo.gif'
}
